<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of propierties_controller
 *
 * @author 250-1-405
 */
class PropertiesController extends AppController {

    //put your code here
    var $name = "Properties";

    function index() {
        $this->layout = "ajax";
        $id = $this->Session->read('proy_id');
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } else {
            $this->Property->recursive = -1;
            $this->set("properties", $this->Property->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('Property.id', 'Property.codigo', 'Property.nombre', 'Property.vereda', 'Property.area_negociacion', 'Property.area_titulo'))));
            $this->set('cod', $this->Session->read('cod'));
        }
    }

    function property_index() {
        $this->layout = "ajax";
        $id = $this->Session->read('proy_id');
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } elseif (($this->Session->read('fase1') == 1) OR ($this->Session->read('fase1') == 2)) {
            $this->Property->recursive = -1;
            $this->set("properties", $this->Property->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('Property.id', 'Property.codigo', 'Property.nombre'))));
            $this->set('cod', $this->Session->read('cod'));
        } else {
            $this->Session->setFlash('Este Proyecto No Superó Fase Uno');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        }
    }

    function index_quality_control() {
        $this->layout = "ajax";
        $id = $this->Session->read('proy_id');
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } elseif (($this->Session->read('fase1') == 1) OR ($this->Session->read('fase1') == 2)) {
            $this->Property->recursive = -1;
            $this->set("properties", $this->Property->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('Property.id', 'Property.codigo', 'Property.nombre'))));
            $this->set('cod', $this->Session->read('cod'));
        } else {
            $this->Session->setFlash('Este Proyecto No Superó Fase Uno');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        }
    }

    function index_topographic_map() {
        $this->layout = "ajax";
        $id = $this->Session->read('proy_id');
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } elseif (($this->Session->read('fase1') == 1) OR ($this->Session->read('fase1') == 2)) {
            $this->Property->recursive = -1;
            $this->set("properties", $this->Property->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('Property.id', 'Property.codigo', 'Property.nombre'))));
            $this->set('cod', $this->Session->read('cod'));
        } else {
            $this->Session->setFlash('Este Proyecto No Superó Fase Uno');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        }
    }

    function index_appraisal() {
        $this->layout = "ajax";
        $id = $this->Session->read('proy_id');
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } elseif (($this->Session->read('fase1') == 1) OR ($this->Session->read('fase1') == 2)) {
            $this->Property->recursive = -1;
            $this->set("properties", $this->Property->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('Property.id', 'Property.codigo', 'Property.nombre'))));
            $this->set('cod', $this->Session->read('cod'));
        } else {
            $this->Session->setFlash('Este Proyecto No Superó Fase Uno');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        }
    }

    function property_edit($id = null) {
        $this->layout = "ajax";
        $cod = $this->Session->read('cod');
        $this->set('cod', $cod);
        $city = $this->Property->field('city_id', array('id' => $id));
        $dp = $this->Property->City->field('departament_id', array('id' => $city));
        $this->set('departaments', $this->Property->City->Departament->find('list', array('order' => 'name ASC')));
        $this->set('cities', $this->Property->City->find('list', array('order' => 'name ASC', 'conditions' => array('City.departament_id' => $dp))));
        $this->set('dp', $dp);
        $this->Property->recursive = -1;
        $this->set('codigo', $this->Property->find("first", array("conditions" => array("Property.id" => $id), "fields" => array("Property.codigo"))));

        if (!empty($this->data)) {
            if ($this->Property->saveAll($this->data)) {
                $this->Session->setFlash('Los datos del predio se han guardado con éxito');
                $this->redirect(array('controller' => 'Properties', 'action' => 'property_index'));
            } else {
                $this->Session->setFlash('Error guardando datos');
            }
        } else {
            $this->Property->recursive = -1;
            $this->data = $this->Property->find("first", array("conditions" => array("Property.id" => $id)));
        }
    }

    function add() {
        $this->layout = "ajax";
        $id = $this->Session->read('proy_id');
        $cod = $this->Session->read('cod');
        $this->set('est', $this->Session->read('estado'));
        $this->set('cod', $cod);
        if (!empty($this->data)) {

            $max = $this->Property->find('first', array('order' => 'Property.code_index DESC', 'conditions' => array('Property.proyect_id' => $id)));

            if (isset($max['Property']['code_index'])) {
                $cont = $max['Property']['code_index'];
            } else {
                $cont = 0;
            }
            $cont++;
            $str = $this->ceros($cont);
            $codPredio = "$cod-$str";

            $this->data['Property']['codigo'] = $codPredio;
            $this->data['Property']['code_index'] = $cont;
            if ($this->Property->save($this->data)) {
                $this->Session->setFlash('Predio guardado con exito');
                $this->redirect(array('controller' => 'Properties', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Error guardando datos');
                $this->set('departaments', $this->Property->City->Departament->find('list', array('order' => 'name ASC')));
            }
        } else {
            $this->data['Property']['proyect_id'] = $id;
            $this->set('departaments', $this->Property->City->Departament->find('list', array('order' => 'name ASC')));
        }
    }

    function select() {
        $this->set('est', $this->Session->read('estado'));
        $this->set('cities', $this->Property->City->find('list', array(
                    'order' => 'name ASC',
                    'conditions' => array('City.departament_id' => $this->data['Property']['departament_id'])
                        )
                ));
    }

    function ceros($cod) {
        if ($cod >= 10 && $cod < 100) {
            return "0" . $cod;
        } else if ($cod < 10) {
            return "00" . $cod;
        } else if ($cod >= 100) {
            return $cod;
        }
    }

    function edit($id) {
        $this->set('est', $this->Session->read('estado'));
        $cod = $this->Session->read('cod');
        $this->set('cod', $cod);
        $city = $this->Property->field('city_id', array('id' => $id));
        $dp = $this->Property->City->field('departament_id', array('id' => $city));
        $this->set('departaments', $this->Property->City->Departament->find('list', array('order' => 'name ASC')));
        $this->set('cities', $this->Property->City->find('list', array('order' => 'name ASC', 'conditions' => array('City.departament_id' => $dp))));
        $this->set('dp', $dp);
        $this->Property->recursive = -1;
        if (empty($this->data)) {

            $this->data = $this->Property->find('first', array('conditions' => array('Property.id' => $id)));
        } else {
            if ($this->Property->save($this->data)) {
                $this->Session->setFlash('Datos actualizados');
                $this->redirect(array('controller' => 'Properties', 'action' => 'index'));
            }
        }
    }

    function delete($id) {

        $idp = $this->Property->TitleStudy->field('TitleStudy.id', array('TitleStudy.property_id' => $id));
        App::import('model', 'TitleStudy');
        $title = new TitleStudy();
        $title->RequirementsTitleStudy->query("DELETE FROM requirements_title_studies where title_study_id=$idp");
        $this->Property->deleteAll(array('Property.id' => $id), true);
        $this->Session->setFlash('Predio borrado exitosamente');
        $this->redirect(array('controller' => 'Properties', 'action' => 'index'));
    }

    function index2() {
        $this->layout = "ajax";
        $id = $this->Session->read('proy_id');
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } else {

            $this->Property->recursive = -1;
            $this->set("properties", $this->Property->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('Property.id', 'Property.codigo', 'Property.nombre', 'Property.vereda', 'Property.area_negociacion', 'Property.area_titulo'))));
            $this->set('cod', $this->Session->read('cod'));
        }
    }

    function initial_evaluation_index() {

        $proyect_id = $this->Session->read('proy_id');
        $this->Property->recursive = -1;
        //Configure::write('debug',0);
        $this->set('proyect_id', $proyect_id);
        $predios = $this->Property->find('all', array(
            'fields' => array('Property.*', 'Departament.name', 'City.name'),
            'conditions' => array('Property.proyect_id' => $proyect_id),
            'joins' => array(
                array('table' => 'cities', 'alias' => 'City', 'type' => 'left', 'conditions' => array('City.id=Property.city_id')),
                array('table' => 'departaments', 'alias' => 'Departament', 'type' => 'left', 'conditions' => array('Departament.id=City.departament_id'),),
            )
                ));
        $this->set('predios', $predios);
    }

    function final_review_index() {

        $proyect_id = $this->Session->read('proy_id');

        App::Import('model', 'Proyect');
        $proyect = new Proyect();
        $publicado = $proyect->field('Proyect.publicado', array('Proyect.id' => $proyect_id));

        if ($publicado == 1) {
            $this->Property->recursive = -1;
            $this->set('proyect_id', $proyect_id);
            $predios = $this->Property->find('all', array(
                'fields' => array('Property.codigo', 'Property.id', 'Property.nombre', 'Property.matricula', 'Property.oficina_instrumentos', 'Departament.name', 'City.name'),
                'conditions' => array('Property.proyect_id' => $proyect_id),
                'joins' => array(
                    array('table' => 'cities', 'alias' => 'City', 'type' => 'left', 'conditions' => array('City.id=Property.city_id')),
                    array('table' => 'departaments', 'alias' => 'Departament', 'type' => 'left', 'conditions' => array('Departament.id=City.departament_id'),),
                )
                    ));
            $this->set('predios', $predios);
        } else {
            $this->Session->setFlash('Este proyecto aún no ha sido publicado.', 'flash_custom');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        }
    }

}

?>
